﻿using Janus.BaseEntitys;
using Janus.Mes.DTO;
using Janus.Mes.IDal;
using Janus.Mes.Repositories.iBatis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Collections;
using Janus.Cores.Tools;
using Janus.Cores.Extension;

namespace Janus.Mes.SqlDal
{
    public class BaseDal<POCO, DTO> : iBatisRepository<POCO>, IBaseDal<POCO, DTO> where POCO : Entity, new() where DTO : BaseDTO
    {
        public new bool Add<DTO, POCO>(List<DTO> ts)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = AutoMapper.Mapper.DynamicMap<List<POCO>>(ts);
            return base.Add(poco);
        }

        public new bool Add<DTO, POCO>(DTO t)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = AutoMapper.Mapper.DynamicMap<POCO>(t);
            return base.Add(poco);
        }

        public new ResponseResult<IList<DTO>> FindAll<POCO, DTO>(SearchEntity<POCO> searchEntity)
            where POCO : Entity, new()
            where DTO : BaseDTO, new()
        {
            var result = base.FindAll<POCO>(searchEntity);
            return AutoMapper.Mapper.DynamicMap<ResponseResult<IList<DTO>>>(result);
        }

        public new IList<DTO> FindAll<POCO, DTO>(Expression<Func<POCO, bool>> predicate, string orderBy = null)
            where POCO : Entity
            where DTO : BaseDTO
        {
            var result = base.FindAll<POCO>(predicate, orderBy);
            return AutoMapper.Mapper.DynamicMap<IList<DTO>>(result);
        }

        public new IList<DTO> FindAll<POCO, DTO>(string predicate, string orderBy = null)
            where POCO : Entity
            where DTO : BaseDTO
        {
            var result = base.FindAll<POCO>(predicate, orderBy);
            return AutoMapper.Mapper.DynamicMap<IList<DTO>>(result);
        }

        public new IList<DTO> FindAll<POCO, DTO>(Querier predicate)
            where POCO : Entity
            where DTO : BaseDTO
        {
            var result = base.FindAll<POCO>(predicate);
            return AutoMapper.Mapper.DynamicMap<IList<DTO>>(result);
        }

        public new ResponseResult<IList<DTO>> FindAllByPro<POCO, DTO>(PageCriteria pageCriteria)
            where POCO : Entity, new()
            where DTO : BaseDTO, new()
        {
            var result = base.FindAllByPro<POCO>(pageCriteria);
            return AutoMapper.Mapper.DynamicMap<ResponseResult<IList<DTO>>>(result);
        }

        public new DTO FindOne<POCO, DTO>(Expression<Func<POCO, bool>> predicate)
            where POCO : Entity
            where DTO : BaseDTO
        {
            var result = base.FindOne<POCO>(predicate);
            return AutoMapper.Mapper.DynamicMap<DTO>(result);
        }

        public new DTO FindOne<POCO, DTO>(string predicate)
            where POCO : Entity
            where DTO : BaseDTO
        {
            var result = base.FindOne<POCO>(predicate);
            return AutoMapper.Mapper.DynamicMap<DTO>(result);
        }

        public new DTO FindOne<POCO, DTO>(Querier predicate)
           where POCO : Entity
           where DTO : BaseDTO
        {
            var result = base.FindOne<POCO>(predicate);
            return AutoMapper.Mapper.DynamicMap<DTO>(result);
        }

        public new DTO FindOne<POCO, DTO>(params object[] entity)
            where POCO : Entity
            where DTO : BaseDTO
        {
            var result = base.FindOne<POCO>(entity);
            return AutoMapper.Mapper.DynamicMap<DTO>(result);
        }

        public new bool Save<DTO, POCO>(List<DTO> entitys)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = AutoMapper.Mapper.DynamicMap<List<POCO>>(entitys);
            return base.Save<POCO>(poco);
        }

        public new int Save<DTO, POCO>(DTO t)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = AutoMapper.Mapper.DynamicMap<POCO>(t);
            return base.Save<POCO>(poco);
        }

        public new bool Update<DTO, POCO>(List<DTO> entitys)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = AutoMapper.Mapper.DynamicMap<List<POCO>>(entitys);
            return base.Update<POCO>(poco);
        }

        public new int Update<DTO, POCO>(DTO t)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = AutoMapper.Mapper.DynamicMap<POCO>(t);
            return base.Update<POCO>(poco);
        }

        public new int UpdateSetion<DTO, POCO>(DTO entity, Conditioner conditions)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = AutoMapper.Mapper.DynamicMap<POCO>(entity);
            return base.UpdateSetion<POCO>(poco, conditions);
        }


        public new int UpdateSetion<DTO, POCO>(DTO entity, Querier conditions)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = AutoMapper.Mapper.DynamicMap<POCO>(entity);
            return base.UpdateSetion<POCO>(poco, conditions);
        }

        public new bool BatchInset<DTO, POCO>(List<DTO> models)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = models.ToEntitys<POCO>();
            return base.BatchInset<POCO>(poco);
        }

        public new bool BatchUpdate<DTO, POCO>(List<DTO> models)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = models.ToEntitys<POCO>();
            return base.BatchUpdate<POCO>(poco);
        }

        public new bool BatchSave<DTO, POCO>(List<DTO> models)
            where DTO : BaseDTO
            where POCO : Entity
        {
            var poco = models.ToEntitys<POCO>();
            return base.Save<POCO>(poco);
        }
    }
}
